package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class JDBCDemo6 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("欢迎登陆");
        System.out.println("请输入用户名:");
        String username = sc.nextLine();
        System.out.println("请输入密码:");
        String password = sc.nextLine();
        try (
                Connection connection = DBUtil.getConnection();
        ) {
            Statement statement = connection.createStatement();
            /*
            select id,username,password,nickname,age
            from userinfo
            where username='"+username+"'
            and password='"+password+"'
             */
            String sql = "select id,username,password,nickname,age " +
                         "from userinfo " +
                         "where username='"+username+"'" +
                         "and password='"+password+"'";
            ResultSet rs=statement.executeQuery(sql);
            if (rs.next()){
                String nickname=rs.getString("nickname");
                System.out.println("登陆成功！欢迎回来."+nickname);
            }else{
                System.out.println("登陆失败！用户名或密码错误");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
